package com.main.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.generico.dao.ctg.CtgProductoDao;
import com.generico.dto.ctg.CtgProducto;
import com.generico.dto.ctg.CtgSucursal;
import com.generico.form.dto.RegistroCliente;
import com.main.cliente.dto.SgiPersona;
import com.main.dao.SgiPersonaDao;
import com.main.dao.UserDAO;
import com.main.java.CtgDepartamento;
import com.main.java.CtgMunicipio;
import com.main.java.User;
import com.main.mail.GenericoMailSender;
import com.web.util.GenericoUtil;

@Controller
public class SecurityNavigation{
	
	@Autowired
	private CtgProductoDao ctgProductoDao;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private SgiPersonaDao sgiPersonaDao;
	
	@RequestMapping(value="/user-login", method=RequestMethod.GET)
	public ModelAndView loginForm() {
		return new ModelAndView("secureLoggin/login-form");
	}

	@RequestMapping(value="/error-login", method=RequestMethod.GET)
	public ModelAndView invalidLogin() {
		ModelAndView modelAndView = new ModelAndView("secureLoggin/login-form");
		modelAndView.addObject("error", true);
		return modelAndView;
	}
	
//	@RequestMapping(value="/restablecer-clave", method=RequestMethod.GET)
//	public String restablecerClave(@ModelAttribute("sgiPersona") RegistroCliente registroCliente, BindingResult result) {
//		return "reestablecerClave";
//	}
	@RequestMapping(value="/restablecer-clave")
	public ModelAndView restablecerClave(@ModelAttribute("sgiPersona") RegistroCliente registroCliente, BindingResult result){
		ModelAndView modelAndView = new ModelAndView("secureLoggin/reestablecerClave");
		modelAndView.addObject("error", true);
		return modelAndView;
	}
	
	@RequestMapping(value="/reestableciendo-clave")
	public String reestableciendo(@ModelAttribute("sgiPersona") RegistroCliente registroCliente, BindingResult result, ModelMap model) {
		
		try {
		
		if (!registroCliente.getPersonaPrimerNombre().isEmpty()) {
			User usuario = userDAO.getUser(registroCliente.getPersonaPrimerNombre());
			SgiPersona persona = new SgiPersona();
			persona = sgiPersonaDao.obtenerPersonaPorUsuario(usuario.getLogin());
			
			GenericoUtil.getBase64Decode(usuario.getPassword());
			
			GenericoMailSender.enviarMensaje(GenericoUtil.getBase64Decode(usuario.getPassword()), 
					usuario.getLogin(), 
					persona.getPersonaEmail(), 
					"restablecerClave", 
					(persona.getPersonaPrimerNombre() + " " + persona.getPersonaSegundoNombre() + " " + persona.getPersonaPrimerApellido() + " " + persona.getPersonaSegundoApellido()));
			
			model.put("correo", persona.getPersonaEmail());
			
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "secureLoggin/reestableciendo";
	}

	@SuppressWarnings("static-access")
	@RequestMapping(value="/success-login", method={RequestMethod.GET, RequestMethod.POST})
	public String successLogin(HttpServletResponse response) {

		GenericoUtil genericoUtil = new GenericoUtil();
		if (genericoUtil.hasRole("ROLE_CLIENTE")) {
		return "redirect:/clienteHome";
		} else if (genericoUtil.hasRole("ROLE_ADMIN")){
		return "redirect:/administradorHome";
		} else if(genericoUtil.hasRole("ROLE_SECRETARIA")){
			return "redirect:/empleadoHome";
		} else if(genericoUtil.hasRole("ROLE_VENDEDOR")){
			return "redirect:/empleadoHome";
		} else if(genericoUtil.hasRole("ROLE_INSTALADOR")){
			return "redirect:/empleadoHome";
		} else if(genericoUtil.hasRole("ROLE_JEFE_TALLER")){
			return "redirect:/empleadoHome";
		} else if(genericoUtil.hasRole("ROLE_ASISTENTE_GERENTE")){
			return "redirect:/empleadoHome";
		} else if(genericoUtil.hasRole("ROLE_GERENTE")){
			return "redirect:/empleadoHome";
			
		} else {
			return "secureLoggin/success-login";
		}
	}
	
	@SuppressWarnings("static-access")
	@RequestMapping(value="/clienteHome", method={RequestMethod.GET, RequestMethod.POST})
	public ModelAndView clienteHome(HttpServletRequest request, ModelMap model) {
		GenericoUtil genericoUtil = new GenericoUtil();
		model.put("rolUsuario", genericoUtil.hasRole("ROLE_CLIENTE") == true ? "ROLE_CLIENTE":"");
		model.put("usuarioId", genericoUtil.getSgdUsuarioIdFromAcegi());
		model.put("userName", genericoUtil.getUserName());
		model.put("ctgProductos", JSONArray.fromObject(ctgProductoDao.getAllActivesAsArray()));
		return new ModelAndView("cliente/clienteInicio");
	}
	
	@SuppressWarnings("static-access")
	@RequestMapping(value="/administradorHome", method={RequestMethod.GET, RequestMethod.POST})
	public ModelAndView administradorHome(HttpServletRequest request, ModelMap model) {
		GenericoUtil genericoUtil = new GenericoUtil();
		model.put("userName", genericoUtil.getUserName());
		model.put("usuarioId", genericoUtil.getSgdUsuarioIdFromAcegi());
		model.put("rolUsuario", genericoUtil.hasRole("ROLE_ADMINISTRADOR") == true ? "ROLE_ADMINISTRADOR":"");
		return new ModelAndView("admin/home");
	}

//	@RequestMapping(value="/empleadoHome", method={RequestMethod.GET, RequestMethod.POST})
//	public ModelAndView empleadoHome() {
//		return new ModelAndView("empleado/empleadoInicio");
//	}
	
	@RequestMapping(value="/empleadoHome", method={RequestMethod.GET, RequestMethod.POST})
	public ModelAndView empleadoHomeEXTJS() {
		return new ModelAndView("empleado/home");
	}
	
	
}
